package com.zhdl.hardware.motor.leisai.repository.impl;

import com.project.command.faq.event.behavior.BehaviorPerform;
import com.zhdl.common.ytools.callback.CommonCallback;
import com.zhdl.hardware.motor.leisai.repository.LeisaiPortCmdHandleDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 雷赛电机-串口电机层
 */
@Slf4j
@Component
public class LeisaiPortCmdHandleDaoImpl implements LeisaiPortCmdHandleDao {

    @Resource
    public BehaviorPerform behaviorPerform;

    public Map<String, LeisaiMotorCmdHandleDaoImpl> daoMap = new ConcurrentHashMap<>();

    @Resource
    public ThreadPoolTaskScheduler taskScheduler1;

    /**
     * 初始化网口
     *
     * @param port 网口地址
     */
    public void addPort(String port) {
        LeisaiMotorCmdHandleDaoImpl leisaiMotorCmdHandleDao = new LeisaiMotorCmdHandleDaoImpl(port, behaviorPerform);
        daoMap.put(port, leisaiMotorCmdHandleDao);
    }

    /**
     * 读取电机当前位置
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     */
    @Override
    public void leisaiReadCurrentLocation(String port, String slaveStationId, CommonCallback<String> callback) {
        daoMap.get(port).leisaiReadCurrentLocation(slaveStationId, s -> {
            callback.statusCallback(s);
            log.info("雷赛电机当前电机位置为:" + s);
        });
    }

    /**
     * 设置电机移动距离
     *
     * @param port           串口
     * @param slaveStationId 从站Id
     * @param distance       距离
     */
    @Override
    public void leisaiSetMoveDist(String port, String slaveStationId, int distance) {
        daoMap.get(port).leisaiSetMoveDist(slaveStationId, "6201", distance);
        log.info("雷赛电机-端口为_" + port + ",从站为_" + slaveStationId + ",距离为_" + distance);
    }

    /**
     * 雷赛电机-电机移动速度
     *
     * @param slaveStationId 从站Id
     * @param speed          写入值
     */
    @Override
    public void leisaiSetMoveSpeed(String port, String slaveStationId, int speed) {
        daoMap.get(port).leisaiSetMoveSpeed(slaveStationId, "6202", speed);
        log.info("雷赛电机-端口为_" + port + ",从站为_" + slaveStationId + ",速度为_" + speed);
    }

}
